namespace Maple.Branch.Permissions.EfCoreRepositories
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using EfCoreConfigurations;
    using Entities;
    using IRepositories;
    using Microsoft.EntityFrameworkCore;
    using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
    using Volo.Abp.EntityFrameworkCore;

    public class PermissionGrantRepository : EfCoreRepository<IPermissionsDbContext, PermissionGrant, Guid>, IPermissionGrantRepository
    {
        public PermissionGrantRepository(IDbContextProvider<IPermissionsDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public virtual async Task<PermissionGrant> FindAsync(
            string name,
            string providerName,
            string providerKey,
            CancellationToken cancellationToken = default)
        {
            return await DbSet.FirstOrDefaultAsync(s =>
                    s.Name == name
                    && s.ProviderName == providerName
                    && s.ProviderKey == providerKey,
                    GetCancellationToken(cancellationToken)
                );
        }

        public virtual async Task<List<PermissionGrant>> GetListAsync(
            string providerName,
            string providerKey,
            CancellationToken cancellationToken = default)
        {
            return await DbSet.Where(s =>
                    s.ProviderName == providerName
                    && s.ProviderKey == providerKey
                ).ToListAsync(GetCancellationToken(cancellationToken));
        }
    }
}
